﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Wolfy.NetDisk.Core.Security
{
    public static class MD5Hleper
    {
        /// <summary>
        /// 获取字符串的md5
        /// </summary>
        /// <param name="strInput">输入参数</param>
        /// <returns>md5</returns>
        public static string GetMD5FromString(string strInput)
        {
            using (MD5 md5 = MD5.Create())
            {
                StringBuilder sb = new StringBuilder();
                byte[] buffer = Encoding.Default.GetBytes(strInput);
                buffer = md5.ComputeHash(buffer);
                for (int i = 0; i < buffer.Length; i++)
                {
                    sb.Append(buffer[i].ToString("X2"));
                }
                return sb.ToString();
            }
        }
        /// <summary>
        /// 获取文件的md5
        /// </summary>
        /// <param name="strFilePath"></param>
        /// <returns></returns>
        public static string GetMD5FromFile(string strFilePath)
        {
            using (MD5 md5 = MD5.Create())
            {
                using (FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.ReadWrite))
                {
                    StringBuilder sb = new StringBuilder();
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    buffer = md5.ComputeHash(buffer);
                    for (int i = 0; i < buffer.Length; i++)
                    {
                        sb.Append(buffer[i].ToString("X2"));
                    }
                    return sb.ToString();
                }
            }
        }
        /// <summary>
        /// 获取文件的md5
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static string GetMD5FromFile(Stream stream)
        {
            using (MD5 md5 = MD5.Create())
            {
                StringBuilder sb = new StringBuilder();
                byte[] buffer = new byte[stream.Length];
                stream.Read(buffer, 0, buffer.Length);
                buffer = md5.ComputeHash(buffer);
                for (int i = 0; i < buffer.Length; i++)
                {
                    sb.Append(buffer[i].ToString("X2"));
                }
                return sb.ToString();

            }
        }
        /// <summary>
        /// 获取文件的md5
        /// </summary>
        /// <param name="strFilePath"></param>
        /// <returns></returns>
        public static string GetMD5FromFile(byte[] fileBytes)
        {
            using (MD5 md5 = MD5.Create())
            {

                StringBuilder sb = new StringBuilder();

                fileBytes = md5.ComputeHash(fileBytes);
                for (int i = 0; i < fileBytes.Length; i++)
                {
                    sb.Append(fileBytes[i].ToString("X2"));
                }
                return sb.ToString();

            }
        }
    }
}
